
import Dialog from '../../../dist/dialog/dialog';
let app = getApp();
let location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    cstoreId:null, //	商户ID
    authType:0, //权限
    timeType: '', //选择日期
    longitude:'',
    latitude:'',
    groupList:{}, //群数量
    storeInfo:{},
    storeList:[],
    slideBanner:[], //广告轮播图
    distanceInex:2, //选择距离的索引
    distance:3, //默认3KM
    distanceItem:1,
    distanceList: [1,2,3,4,5],
    authTypeName:'',
    distanceShow:false, //选择距离开启
    businessShow:false, //切换商户开启
    couponShow: false,
    couponText:'',
    couponSn: '', //核销券码
    shopList:[ //门店经营列表
      {
        text:'扫码验券',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop1.png',
        navTo:'couponCode'
      },
      {
        text:'输码验券',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop2.png',
        navTo:'couponShow'
      },
      {
        text:'核销明细',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop3.png',
        navTo:'/packageF/consumption/verificationDetails/verificationDetails'
      },
      {
        text:'商品管理',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop4.png',
        navTo:'/packageF/consumption/commodity/commodity'
      },
      {
        text:'售卖明细',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop5.png',
        navTo:'/packageF/consumption/sellDetail/sellDetail'
      },
      {
        text:'客户评价',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop6.png',
        navTo:'/packageF/consumption/clientEvaluation/clientEvaluation'
      },
      {
        text:'核销员管理',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop7.png',
        navTo:'/packageF/consumption/verifierList/verifierList'
      },
      {
        text:'收益管理',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop8.png',
        navTo:'/packageF/consumption/incomeDetail/incomeDetail'
      }
    ],
    shopVerifierList:[ //门店经营列表
      {
        text:'扫码验券',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop1.png',
        navTo:'couponCode'
      },
      {
        text:'输码验券',
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/shop2.png',
        navTo:'couponShow'
      },

    ],
    statsList:[
      {
        price:3,
        text:'核销订单数',
        code:'',
      },
      {
        price:0,
        text:'待核销订单数',
        code:'',
      },
      {
        price:0,
        text:'支付订单数',
        code:'',
      },
      {
        price:0,
        text:'退款订单数',
        code:'',
      },
      {
        price:0,
        text:'会员人数',
        code:'',
      },
      {
        price:0,
        text:'在售商品数',
        code:'',
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    try {
      wx.removeStorageSync('applyaddresspoint');
    } catch (e) {
      // Do something when catch error
    }
    this._getLocation((mapdata,e) => {
      this.setData({
        longitude: e.lng,
        latitude: e.lat
      });
      if((options.cstoreId??'') !== '') {
        this.setData({
          cstoreId: Number(options.cstoreId)
        })
      }
      this.businessData();
    },(error) => {
      console.log(error);
      wx.showToast({
        icon: 'none',
        title: '请开启定位授权',
        duration: 1500
      });
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      },1000);
      
    });
  },
  onShow(){
    try {
      let value = wx.getStorageSync('applyaddresspoint');
      if (value) {
        this.setData({
          longitude: value.point.lng,
          latitude: value.point.lat,
        });
        // Do something with return value
      }
    } catch (e) {
      console.log(e);
    }
  },
  businessData() { //商户助手数据
    let that = this;
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.index");
    let json = {
      cstore_id:this.data.cstoreId,
      distance:this.data.distance,
      longitude:this.data.longitude,
      latitude: this.data.latitude,
      time_type:this.data.timeType
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result == 1) {
          let count = res.data.count;
          let statsList = that.data.statsList;
          Object.keys(count).forEach((item,index) => { //循环数据统计对象
            statsList[index].code = item;
            statsList[index].price = count[item];
          })
          that.setData({
            statsList:statsList,
            authTypeName : res.data.auth_type_name,
            authType : res.data.auth_type,
            slideBanner : res.data.slide,
            groupList : res.data.group_list,
            storeList : res.data.store_list,
            cstoreId : res.data.store.id,
            storeInfo : res.data.store
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onClose(event) {
    let type = event.currentTarget.dataset.type;
    if(type === 'distance') {
      this.setData({
        distanceShow:false
      })
    } else if(type === 'couponCode') {
      this.setData({
        couponShow:false
      })
    }  else {
      this.setData({
        businessShow:false
      })
    }
  },
  onOpen(event) {
    let type = event.currentTarget.dataset.type;
    if(type === 'distance') {
      this.setData({
        distanceShow:true
      })
    } else if(type === 'couponCode') {
      this.setData({
        couponShow:false
      })
    } else {
      this.setData({
        businessShow:true
      })
    }
  },
  bindCoupon(event) {
    this.setData({
      couponText: event.detail.value
    })
  },
  onAffirm() {
    this.setData({
      distanceShow:false,
      distance:this.data.distanceItem
    })
    this.businessData();
  },
  onCouponAffirm() { //提交券码
    if(this.data.couponText) {
      this.setData({
        couponSn: this.data.couponText
      })
      Dialog.confirm({
        title: '提示',
        message: `是否确认核销消费券号：${this.data.couponSn}`,
      })
        .then(() => {
          let that = this;
          let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.examineCoupon");
          let json = {
            cstore_id:this.data.cstoreId,
            coupon_sn: this.data.couponSn,
            request_type:2
          };
          app._postNetWork({
            url: urlStr,
            data:json,
            success: function (resdata) {
              let res = resdata.data;
              if (res.result == 1) {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
                that.setData({
                  couponShow: false,
                  couponText: ''
                })
              } else {
                Dialog.alert({
                  title: '提示',
                  message: res.msg,
                }).then(() => {
                  // on close
                });
              }
            },
            fail: function (res) {
              console.log(res);
            }
          });
        })
        .catch(() => {
          // on cancel
        });
    } else {
      wx.showToast({
        icon: 'none',
        title: '请输入消费券编码',
        duration: 1500
      });
    } 
 
  },
  onActiveStore(event) {//切换商户
    let id = event.currentTarget.dataset.id;
    wx.redirectTo({
      url: `/packageF/consumption/business/business?cstoreId=${id}`
    })
  },
  onDistance(event) { //选择不同距离
    const e = event.currentTarget.dataset,
          index = e.index,
          item  = e.item;
    this.setData({
      distanceInex: index,
      distanceItem:item
    })
  },
  slideNavTo(event) {
    let navTo = event.currentTarget.dataset.navto;
    wx.navigateTo({
      url: navTo,
    })
  },
  timeBusiness(event) {
    let timeType = event.currentTarget.dataset.timetype;
    if(this.data.timeType !== timeType) { //避免重复请求数据
      this.setData({
        timeType:timeType
      })
      this.businessData()
    }
  },
  shopItemOn(event) { //跳转
    let navTo = event.currentTarget.dataset.navto;
    if(navTo === 'couponShow') {
      this.setData({
        couponShow: true
      })
    }  else if (navTo === 'couponCode') {
        wx.scanCode({
          success: (res) => {
            wx.navigateTo({
              url: `/packageF/consumption/verification/verification?storeId=${this.data.cstoreId}&couponSn=${res.result}`
            })
          }
        })
    } else {
      wx.navigateTo({
        url: `${navTo}?storeId=${this.data.cstoreId}`
      })
    }
     
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
   
  }
})